package core

import (
	"animal/octopus/cache"
	"animal/octopus/common"
	"animal/octopus/orm"

	log "github.com/sirupsen/logrus"
	"gorm.io/gorm"
)

// ------------------------------------------------------------------------------
//
// ------------------------------------------------------------------------------
func GetDependencyList(db *gorm.DB, moduleID uint) []common.DependencyInfo {
	return cache.Instance().GetDependencyList(moduleID)
}

// ------------------------------------------------------------------------------
//
// ------------------------------------------------------------------------------
func CreateDependency(db *gorm.DB, moduleID uint, depInfo common.DependencyInfo) error {
	if err := common.DB(db).Transaction(func(tx *gorm.DB) error {
		depID, err := orm.CreateDependency(tx, moduleID, depInfo)
		if err != nil {
			log.WithFields(log.Fields{
				"module":  moduleID,
				"depInfo": common.MustMarshal(depInfo),
			}).Error("create dependency in database failed")
			return common.NewErrCode(common.ErrGeneralError)
		}

		if err := cache.Instance().CreateDependency(moduleID, depID, depInfo); err != nil {
			log.WithFields(log.Fields{
				"module":  moduleID,
				"depInfo": common.MustMarshal(depInfo),
			}).Error("create dependency in cache failed")
			return err
		}

		return nil
	}); err != nil {
		log.Error("create dependency failed")
		return err
	}

	return nil
}

// ------------------------------------------------------------------------------
//
// ------------------------------------------------------------------------------
func UpdateDependency(db *gorm.DB, moduleID uint, depID uint, depInfo common.DependencyInfo) error {
	if err := common.DB(db).Transaction(func(tx *gorm.DB) error {
		if err := orm.UpdateDependency(tx, depID, depInfo); err != nil {
			log.WithFields(log.Fields{
				"module": moduleID,
				"dep":    depID,
				"info":   common.MustMarshal(depInfo)}).
				Error("update dependency in database failed")
			return err
		}
		if err := cache.Instance().UpdateDependency(moduleID, depID, depInfo); err != nil {
			log.WithFields(log.Fields{
				"module": moduleID,
				"dep":    depID,
				"info":   common.MustMarshal(depInfo)}).
				Error("update dependency in cache failed")
			return err
		}
		return nil
	}); err != nil {
		log.Error("update dependency failed")
		return err
	}

	return nil
}

// ------------------------------------------------------------------------------
//
// ------------------------------------------------------------------------------
func DeleteDependency(db *gorm.DB, moduleID uint, depID uint) error {
	if err := common.DB(db).Transaction(func(tx *gorm.DB) error {
		if err := orm.DeleteDependency(tx, depID); err != nil {
			log.WithFields(log.Fields{
				"module":     moduleID,
				"dependency": depID,
			}).Error("delete dependency from database failed")
			return err
		}
		if err := cache.Instance().DeleteDependency(moduleID, depID); err != nil {
			log.WithFields(log.Fields{
				"module":     moduleID,
				"dependency": depID,
			}).Error("delete dependency from cache failed")
			return err
		}
		return nil
	}); err != nil {
		log.Error("delete dependency failed")
		return err
	}

	return nil
}
